package com.eightbears.beboy.base.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.socks.library.KLog;

/**
 * Created by Administrator on 2017/10/16.
 * myClient仪表盘
 */

public class DialChartView extends View {
    private int width;
    private int height;
    private int mRadius;
    //屏幕密度
    private float mDensityDpi;

    private Paint mPaintCircle;
    private Paint mPaintCarve;
    private Paint mPaintText;

    public DialChartView(Context context) {
        this(context, null);
    }

    public DialChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DialChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;
        //像素密度相对于标准320的比例
        mDensityDpi = displayMetrics.densityDpi / 320;

        mPaintCircle = new Paint();
        mPaintCircle.setStyle(Paint.Style.STROKE);
        mPaintCircle.setAntiAlias(true);
        mPaintCircle.setStrokeWidth(2);
        mPaintCircle.setColor(Color.parseColor("#4d7aad"));
        mPaintCircle.setPathEffect(new DashPathEffect(new float[]{4, 4}, 0));//绘制长度为4的实线后再绘制长度为4的空白区域，0位间隔

        mPaintCarve = new Paint();
        mPaintCarve.setStyle(Paint.Style.FILL);
        mPaintCarve.setAntiAlias(true);
        mPaintCarve.setStrokeWidth(6);
        mPaintCarve.setColor(Color.parseColor("#4d7aad"));

    }
    //当自定义控件的尺寸已经决定好的时候回调


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRadius = w / 2;
        //400
        this.width = w / 4;
        this.height = w / 4;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        //移动坐标系基准点也跟着移动
        canvas.translate(width, height);
        //画外圆
        canvas.drawCircle(width, height, mRadius - 5, mPaintCircle);
        //画刻度
        drawCarve(canvas);
        //画字体
//        drawText(canvas);
//        //设置总客户数
//        drawClientSum(canvas);
    }

    private void drawClientSum(Canvas canvas) {
        mPaintText.setTextSize(50);
        mPaintText.setColor(Color.parseColor("#4d7aad"));
        mPaintText.setStyle(Paint.Style.FILL);
        canvas.drawText("21", width / 2 + 25, height, mPaintText);
    }

    private void drawText(Canvas canvas) {
        canvas.drawText("累计客户数", width / 2 - 20, height + 50, mPaintText);
    }

    //画刻度线
    private void drawCarve(Canvas canvas) {
        for (int i = 0; i < 60; i++) {
            if (i % 5 == 0) {
                canvas.drawLine(width - mRadius + 10 * mDensityDpi, height, width - mRadius + 30 * mDensityDpi, height, mPaintCarve);
            } else {
//                canvas.drawLine(width - mRadius + 10 * mDensityDpi, height, width - mRadius + 30 * mDensityDpi, height, mPaintCarve);
//                canvas.drawLine(width, height - width, width, height - width + 30, mPaintCircle);
            }
            canvas.rotate(6, width, height);
        }
    }
}
